using huqiang.Core.HGUI;
using System;
using UnityEngine;
using huqiang.UIComposite;
using huqiang.UIEvent;
using huqiang;
using huqiang.Data;
using huqiang.UIModel;
using System.IO;
using DataControll;
using DataHandle;

public class ConsolePage:UIPage
{
    //反射UI界面上的物体
    class View
    {
        public DockPanel DockPanel;
        public AppendText Console;
        public InputBox InputBox;
    }
    bool ShowLog = true;
    View view;
    public override void Initial(UIElement parent, object dat = null)
    {
        base.Initial(parent, dat);
        view = LoadUI<View>("baseUI", "ConsolePage");//"baseUI"创建的bytes文件名,"page"为创建的页面名
        DockPanelLine.CursorX = Resources.Load<Texture2D>("StretchWX");
        DockPanelLine.CursorY = Resources.Load<Texture2D>("StretchWY");
        InitialUI();
        KcpDataControll.Instance.OpenServer(9998);
    }
    void InitialUI()
    {
        var input = view.DockPanel.MainArea.AddArea(DockpanelArea.Dock.Down, 100, "input");
        view.Console.Enity.SetParent(view.DockPanel.MainArea.model);
        view.Console.Enity.marginType = MarginType.Margin;
        view.InputBox.Enity.SetParent(input.model);
        view.InputBox.Enity.marginType = MarginType.Margin;
        view.DockPanel.Refresh();

        view.InputBox.OnSubmit = (o)=>{
            string s = o.InputString;
            switch(s.ToLower())
            {
                case "end":
                    view.Console.End();
                    break;
                case "clear":
                    view.Console.Clear();
                    break;
                default:
                    view.Console.WriteLine(s);
                    break;
            }
            o.InputString = "";
        };
        view.InputBox.OnDone = (o) => {
            //view.Console.WriteLine(o.InputString);
            //o.InputString = "";
        };
         
    }
    //语言切换功能用
    public override bool ChangeLanguage()
    {
        return base.ChangeLanguage();
    }
    //接收消息
    public override void Cmd(int cmd, object dat)
    {
        switch(cmd)
        {
           case  MsgCmd.Log:
                if(ShowLog)
                {
                    LogData log = dat as LogData;
                    if(log!=null)
                    {
                        view.Console.WriteLine(log.addr);
                        view.Console.WriteLine(log.type.ToString());
                        view.Console.WriteLine(log.condition);
                        view.Console.AddString(log.stackTrace);
                    }
                }
                break;
        }
    }
    //页面弹窗
    public override T PopUpWindow<T>(object obj = null)
    {
        return base.PopUpWindow<T>(obj);
    }
    public override void Update(float time)
    {
        KcpDataControll.Instance.ExecuteMainMission();
    }
}
